﻿using System;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.Collections.Generic;
using ThapCam.System;
using ThapCam.Data.Collection;
using System.Web.Caching;

namespace ThapCam.Ulti
{
    public class CacheManager
    {
        private ThapCamApplication _appControler;

        public CacheManager(ThapCamApplication appControler)
        {
            _appControler = appControler;
        }

        private Cache cache
        {
            get
            {
                return _appControler.Context == null ? null : _appControler.Context.Cache;                
            }
        }

        public void addValue(string key, object value)
        {
            try
            {
                DateTime absoluteExpiration = DateTime.Now;               
                absoluteExpiration = absoluteExpiration.AddMinutes(double.Parse(WebConfig.CACHE_TIME_EXPIRES));                
                cache.Add(key, value, null, absoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Default, null);
            }
            catch (Exception ex)
            {
                LogManager.writeError(typeof(SessionManager).GetType(), null, ex);
            }
        }

        public object getValue(string key)
        {
            try
            {
                return cache.Get(key);
            }
            catch (Exception ex)
            {
                LogManager.writeError(typeof(SessionManager).GetType(), null, ex);
            }
            return null;
        }
    }
}